package util;



import java.io.File;
import java.util.Properties;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;



/*
 * 功能：实现从QAD服务器上下载文件到本�?
 */
public class SFtpUtil {

//	private final static Log logger = LogFactory.getLog(SFtpUtil.class);

	/*
	 * public static void main(String[] args) throws JSchException{ String
	 * ftpHost = "10.6.129.51"; String ftpUserName = "dragon"; String
	 * ftpPassword = "dragon"; int ftpPort = 22; String ftpPath =
	 * "/home/dragon"; String localPath = "D:/迅雷下载"; String fileName =
	 * "159.prn"; sFtpUtil.downloadSftpFile(ftpHost, ftpUserName, ftpPassword,
	 * ftpPort, ftpPath, localPath, fileName); }
	 */

	/*
	 * 从SFTP服务器下载文�?
	 * 
	 * @param ftpHost SFTP IP地址
	 * 
	 * @param ftpUserName SFTP 用户�?
	 * 
	 * @param ftpPassword SFTP用户名密�?
	 * 
	 * @param ftpPort SFTP端口
	 * 
	 * @param ftpPath SFTP服务器中文件�?在路�? 格式�? ftptest/aa
	 * 
	 * @param localPath 下载到本地的位置 格式：H:/download
	 * 
	 * @param fileName 文件名称
	 */
	public static void downloadSftpFile(String ftpPath,String fileName) throws JSchException {
		String ftpHost = "10.6.129.51";
		String ftpUserName = "dragon";
		String ftpPassword = "dragon";
		int ftpPort = 22;
		String localPath = "C:/download";
		Session session = null;
		Channel channel = null;
		JSch jsch = new JSch();
		session = jsch.getSession(ftpUserName, ftpHost, ftpPort);
		session.setPassword(ftpPassword);
		session.setTimeout(100000);
		Properties config = new Properties();
		config.put("StrictHostKeyChecking", "no");
		session.setConfig(config);
		session.connect();

		channel = session.openChannel("sftp");
		channel.connect();
		ChannelSftp chSftp = (ChannelSftp) channel;

		String ftpFilePath = ftpPath + "/" + fileName;
		String localFilePath = localPath + File.separatorChar + fileName;

		try {
			chSftp.get(ftpFilePath, localPath);
		} catch (Exception e) {
			e.printStackTrace();
//			logger.info("download error.");
		} finally {
			chSftp.quit();
			channel.disconnect();
			session.disconnect();
		}

	}

}
